package com.qf.bbapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.bbapi.dao.OrderDao;
import com.qf.bbapi.dao.OrderDetailDao;
import com.qf.bbapi.dao.ShopCarDao;
import com.qf.bbapi.dto.OrderAddTypeDto;
import com.qf.bbapi.dto.OrderDto;
import com.qf.bbapi.dto.ShopCarInfoDto;
import com.qf.bbapi.entity.Order;
import com.qf.bbapi.entity.OrderDetail;
import com.qf.bbapi.entity.ShopCar;
import com.qf.bbapi.service.intf.OrderDetailService;
import com.qf.bbapi.service.intf.OrderService;
import com.qf.bbapi.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/***
 * _ooOoo_
 * o8888888o
 * 88" . "88
 * (| -_- |)
 *  O\ = /O
 * ___/`---'\____
 * .   ' \\| |// `.
 * / \\||| : |||// \
 * / _||||| -:- |||||- \
 * | | \\\ - /// | |
 * | \_| ''\---/'' | |
 * \ .-\__ `-` ___/-. /
 * ___`. .' /--.--\ `. . __
 * ."" '< `.___\_<|>_/___.' >'"".
 * | | : `- \`.;`\ _ /`;.`/ - ` : | |
 * \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 * `=---='
 *          .............................................
 *           佛曰：bug泛滥，我已瘫痪！
 */

@Service
public class OrderServiceImpl extends ServiceImpl<OrderDao,Order> implements OrderService {
    @Resource
    private OrderDao orderDao;
    @Resource
    private ShopCarDao dao;
    @Resource
    private OrderDetailService service;


    @Override
    public R selectByState(int token, int flag) {
        return R.ok(orderDao.selectByState(token,flag));
    }

    @Override
    public R updateState(int token, Integer flag, String orderNo) {
        if (orderDao.updateState(token, flag, orderNo)>0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    @Transactional
    @Override
    public R orderSave(int token, OrderAddTypeDto dto) {
        if (token > 0 && dto != null){
            //查询出总价和数量
            Order order = orderDao.orderInfoOne(dto);
            Order order1 = new Order((long) token,order.getTotalPrice(),order.getTotalNum(),dto.getAddressId());
            int insert = orderDao.insert(order1);
            if (insert > 0){
                //生成订单明细

                //查询出每个 商品的 id 和 个数
                List<ShopCar> shopCars = dao.selectList(new QueryWrapper<ShopCar>().in("id", dto.getShopCarIds()));
                List<OrderDetail> list = new ArrayList<>();
                for (ShopCar shopCar : shopCars) {
                    System.out.println(shopCar);
                    OrderDetail orderDetail = new OrderDetail(order1.getOrderNo(), shopCar.getGoodsId(), shopCar.getGoodsNum());
                    list.add(orderDetail);
                }
                //订单详情 批量添加
                if (service.saveBatch(list)){
                    //订单生成成功 删除购物车记录
                    if (dao.deleteBatchIds(dto.getShopCarIds()) > 0){
                        return R.ok();
                    }

                }
            }
        }
        return R.fail();
    }
}
